package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {


  /*
  步骤分析：
  1.是否需要开启事务
  2.判断关键数据
  3.创建分页对象
  4.创建查询对象
  5.获取查询结果，做类型转换
  6.封装返回值对象
   */
    @Override
    public PageVO<CourseBaseDTO> queryCourseList(PageRequestParams params, QueryCourseModel model,Long companyId) {

        //  1.是否需要开启事务:查询不需要
        //  2.判断关键数据

        System.out.println(companyId);
        Long pageNo = params.getPageNo();
        Integer pageSize = params.getPageSize();

        if (ObjectUtils.isEmpty(pageNo) || pageNo<1){
            params.setPageNo(PageRequestParams.DEFAULT_PAGE_NUM);
        }

        if (ObjectUtils.isEmpty(pageSize) || pageSize<1){
            params.setPageSize(PageRequestParams.DEFAULT_PAGE_SIZE);
        }

        //  3.创建分页对象
        Page<CourseBase> page = new Page<>(pageNo, pageSize);

        //  4.创建查询对象
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();

        //判断查询条件是否为空
        String courseName = model.getCourseName();

        if (StringUtil.isNotBlank(courseName)){
            queryWrapper.eq(CourseBase::getName,courseName);

        }

        String auditStatus = model.getAuditStatus();

        if (StringUtil.isNotBlank(auditStatus)){

            queryWrapper.eq(CourseBase::getAuditStatus,auditStatus);
        }

        //机构隔离
        queryWrapper.eq(CourseBase::getCompanyId,companyId);

        //只选择使用态
       queryWrapper.eq(CourseBase::getStatus, CommonEnum.USING_FLAG.getCode());
//        queryWrapper.eq(CourseBase::getStatus, 1);
        //  5.获取查询结果，做类型转换

        Page<CourseBase> pageResult = this.page(page, queryWrapper);

        List<CourseBase> courseBaseList = pageResult.getRecords();

        List<CourseBaseDTO> courseBaseDTOList = Collections.emptyList();

        if (!CollectionUtils.isEmpty(courseBaseList)){
             courseBaseDTOList = CourseBaseConvert.INSTANCE.entitys2dtos(courseBaseList);
        }


        //  6.封装返回值对象
        PageVO<CourseBaseDTO> pageVO = new PageVO<>(courseBaseDTOList, pageResult.getTotal(), pageNo, pageSize);


        return pageVO;
    }
}
